﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UISelect : MonoBehaviour
{
    public TextMeshProUGUI time;
    public TextMeshProUGUI roleName;    

    public Button btnLeft;
    public Button btnRight;

    public List<string> roleNames;

    public int roleIndex;

    public RoleShowController roleShow;

    public bool isNew;

    public UIMenu ui;


    public void New()
    {
        isNew = true;
        time.enabled = false;
        for (int i = 0; i < StaticData.listRoleInfo.Count; i++)
        {
            roleNames.Add(StaticData.listRoleInfo[i].asset);
        }
    }

    public void Read()
    {
        isNew = false;
        for (int i = 0; i < GameData.listRoles.Count; i++)
        {
            roleNames.Add(GameData.listRoles[i].roleid);
        }
    }
    public void ShowLift()
    {
        roleIndex--;
        ShowRole();
    }

    public void ShowRight()
    {
        roleIndex++;
        ShowRole();
    }

    public void ShowRole()
    {
        if (roleIndex < 0)
            roleIndex = 0;
        else if(roleIndex > roleNames.Count - 1)
            roleIndex = roleNames.Count - 1;    

        btnLeft.interactable = roleIndex != 0;  
        btnRight.interactable = roleIndex != roleNames.Count - 1;

        roleShow.LoadRole(roleNames[roleIndex]);
        roleName.text = roleNames[roleIndex];

        if (isNew == false)
        {
            time.text = GameData.listRoles[roleIndex].saveTime;
            time.enabled = true;
        }
    }

    private void OnEnable()
    {
        ShowRole(); 
    }

    private void OnDisable()
    {
        roleShow.CloseRole();
    }

    public void CreateRole()
    {

        if (isNew == false)
        {
            GameData._curRoleIndex = roleIndex;
        }
        else
        {
            GameData.Create(roleNames[roleIndex]);
            GameData._curRoleIndex = GameData.listRoles.Count - 1;
            GameData.Save();
        }
        ui.EnterGame();
    }
}
